<script language="javascript">
var selectedTeams = {};
function selectTeam(dom,matchID,teamID) {
  var index = "index" + matchID;
  var originalSelected = selectedTeams[index];
  var inputID = "bet_match_id_" + matchID + "_team_id";
  if(originalSelected) {
    originalDOM = originalSelected.dom;
    originalTeamID  = originalSelected.team_id;
    originalDOM.removeClassName("team-selected");
    document.getElementById(inputID).setValue(''); //clear 
    selectedTeams[index] = null;
    if(originalTeamID == teamID) {
      return;
    }
  }
  selectedTeams[index] = {input:document.getElementById(inputID),dom: dom,team_id: teamID}
  //add css class name
  dom.addClassName("team-selected");
  $(inputID).setValue(teamID);
}

function resetBetsForm(){
  for(k in selectedTeams) {
    var team = selectedTeams[k];
    if(team) {
      team.dom.removeClassName("team-selected");
      team.input.setValue('');
      selectedTeams[k] = null;
    }
  }
  $("bets-form").reset();
}
</script>
<% if @matches.blank? %>
  <div class="empty">Come back later for new matches!</div>
<% else %>
<%form_tag bets_path(:sport_id=>@sport),:id=>"bets-form" do %>
  <% group_by_time @matches,:attr=>:begin_at do |time,matches| %>
    <%= render :partial=>"matches/matches",:locals=>{:matches=>matches,:time=>time} %>
  <% end %>
</br>
<%= will_paginate @matches %>
<%end %>
<% end %>

<% content_for :side do %>
<%= link_to_function "Place Bet","$('bets-form').submit()",:class=>"place-bets",:style=>"background:url(#{facebook_image_url('green_header.gif')}) repeat-x;"%>
<%= link_to_function "Reset","resetBetsForm()",:class=>"reset-bets",:style=>"background:url(#{facebook_image_url('orange_header.gif')}) repeat-x;"%>
<%= render :partial=>"sport_side",:locals=>{:sport=>@sport,:sport_side=>@sport} %>
<% end %>

